package com.demo.shardingsphere.util;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

public class DataSourceUtil {

	private static DruidDataSource dataSource = new DruidDataSource();

	// 设置数据源
	static {
		try {
			//1)创建Properties对象
			Properties prop = new Properties();
			//构造输入流
			Class clazz = DataSourceUtil.class;
			InputStream in = clazz.getResourceAsStream("/jdbc.properties");
			//加载文件
			prop.load(in);

			//读取
			String url = prop.getProperty("url");
			String user = prop.getProperty("user");
			String password = prop.getProperty("password");
			String driverClass = prop.getProperty("driverClass");


			// 设置基础设置;
			dataSource.setDriverClassName(driverClass);
			dataSource.setUsername(user);
			dataSource.setUrl(url);
			dataSource.setPassword(password);


		} catch (Exception e) {
			throw new RuntimeException(e);

		}
	}

	// 获取数据源的方法
	public static DataSource getDataSource() {
		return dataSource;
	}
}